import 'package:flutter/material.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/ui/my/order/order_config.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/xfg_text.dart';

/// Descriptions: 取消订单弹窗
/// 取消订单理由：0=价格有点贵，1=规格/款式/数量拍错，2=收获地址填错，3=商家不支持花呗，4=暂时不需要了，5=其他，6=超时自动取消(取消订单时前端不展示这个选项)
/// User: tangzhentao
/// Date: 4:58 下午 2021/12/7
///
Future<int?> showOrderCancelDialog() async {
  return await showModalBottomSheet<int>(
      context: XfgNav.currentContext,
      isScrollControlled: true,
      builder: (_) => _CancelOrderWidget()
  );
}

class _CancelOrderWidget extends StatelessWidget {
  /// 当前选中监听
  late final ValueNotifier<CancelReason?> _indexNotifier = ValueNotifier(null);

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Container(
        height: XfgSize().screenHeight * 0.65,
        padding: EdgeInsets.only(bottom: XfgSize().bottomBarHeight),
        decoration: BoxDecoration(color: XfgColor.white, borderRadius: BorderRadius.vertical(top: Radius.circular(XfgSize().size10))),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.only(top: XfgSize().size18),
              alignment: Alignment.topCenter,
              child: XfgText('取消订单', fontSize: XfgSize().text20, color: XfgColor.text1D, bold: FontWeight.bold),
            ),
            Container(
              padding: EdgeInsets.only(top: XfgSize().size18),
              alignment: Alignment.topCenter,
              child: XfgText('取消后订单无法恢复，优惠券，红包可退出', fontSize: XfgSize().text14, color: XfgColor.text1D),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size18, XfgSize().size10, XfgSize().size20),
              alignment: Alignment.centerLeft,
              child: XfgText('请选择取消原因', fontSize: XfgSize().text16, color: XfgColor.text2),
            ),
            Expanded(
              child: ValueListenableBuilder<CancelReason?>(
                valueListenable: _indexNotifier,
                builder: (_, select, __) => ListView.builder(
                  itemCount: cancelReasonList.length,
                  itemBuilder: (_, index) {
                    final reason = cancelReasonList[index];
                    return GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        if (!(select == reason)) {
                          _indexNotifier.value = reason;
                        }
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: XfgSize().size10, vertical: XfgSize().size15),
                        child: Row(
                          children: [
                            Expanded(child: XfgText(reason.desc, fontSize: XfgSize().text16, color: XfgColor.text1D)),
                            Image.asset(select == reason ? Res.imagesPayRadioS:Res.imagesPayRadio, width: XfgSize().size18, height: XfgSize().size18)
                          ],
                        ),
                      ),
                    );
                  },
                ),
              ),
            ),
            SizedBox(height: XfgSize().size16),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                TextButton(
                  onPressed: XfgNav.pop,
                  style: ButtonStyle(
                    backgroundColor: WidgetStatePropertyAll(XfgColor.cancelOrderNo),
                    shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(left: Radius.circular(XfgSize().size50)))),
                    fixedSize: WidgetStatePropertyAll(Size(XfgSize().size170, XfgSize().size50))
                  ),
                  child: XfgText('暂不取消', fontSize: XfgSize().text16, color: XfgColor.white),
                ),
                TextButton(
                  onPressed: () {
                    final reason = _indexNotifier.value;
                    if (reason != null) {
                      XfgNav.pop(reason.reason);
                    } else {
                      '请选择取消原因'.info();
                    }
                  },
                  style: ButtonStyle(
                      backgroundColor: WidgetStatePropertyAll(XfgColor.theme),
                      shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(right: Radius.circular(XfgSize().size50)))),
                      fixedSize: WidgetStatePropertyAll(Size(XfgSize().size170, XfgSize().size50))
                  ),
                  child: XfgText('确定取消', fontSize: XfgSize().text16, color: XfgColor.white),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}